package com.hhh.petadopt.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hhh.petadopt.pojo.Sentp;
import com.hhh.petadopt.service.SentpService;
import com.hhh.petadopt.utils.JsonUtils;

/**
 * 送养
 * @author 杜镇声声
 *
 */
@Controller
public class SentPetController {
	
	@Autowired
	private SentpService sentpService;
	
	@RequestMapping(value="/sent/sentList")
	public String showSentList() {
		return "sentPet";
	}
	
	//图片上传
	@RequestMapping(value="/sent/uloadPic")
	@ResponseBody
	public String uploadPic(MultipartFile imgs) throws IllegalStateException, IOException{
		//设置图片名称
		String pname = UUID.randomUUID().toString().replace("-", "");
		//获取原本图片名称
		String originalFilename = imgs.getOriginalFilename();
		//获取图片扩展名
		String extName = FilenameUtils.getExtension(originalFilename);
				
		//将文件存到upload文件夹下
		imgs.transferTo(new File("D:/upload/image/"+pname+"."+extName));
		
		//将图片的名称进行拼接
		String pic = "/pic/"+pname+"."+extName;
		
		String objectToJson = JsonUtils.objectToJson(pic);
		
		//返回拼接好的完整的图片路径
		return objectToJson;
	}
	
	//送养入库
	@RequestMapping(value="/sent/saveSent")
	@ResponseBody
	public String saveSentPet(Sentp sentp) {
		//执行操作
		sentpService.saveSentPet(sentp);
		String str = "成功";
		return JsonUtils.objectToJson(str);
	}

}
